package com.zhucx.parser.htmlsucker;

import com.ruoyi.common.utils.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import javax.print.Doc;
import java.io.IOException;

/**
 *  <p>HtmlSucker 的入口</p>
 *  <p>使用方法:</p>
 *  <code>
 *  HtmlSucker.select(HtmlSucker.MAX_TEXT_EXTRACTOR).parse(html);
 *  </code>
 */
public class HtmlSucker {

    public final static byte MAX_TEXT_EXTRACTOR     = 0x01; //最大文本长度抽取
    public final static byte TEXT_DENSITY_EXTRACTOR = 0x02; //文本密度算法抽取

    public static void main(String[] args) throws IOException {
//        String url = "https://mp.weixin.qq.com/s/fFcFlgdtK9ebW3TtCcIi5Q";
//        String url = "https://mil.news.sina.com.cn/china/2022-05-20/doc-imcwiwst8470552.shtml";
        String url = "https://mil.huanqiu.com/world";
//        System.out.println(HtmlSucker.select(TEXT_DENSITY_EXTRACTOR).parse(url, ""));
    }

    /**
     * 选择不同的算法
     * @param extrator
     * @return
     */
    public final static HtmlSucker select(byte extrator) {
        ContentExtractor extractor = null;
        switch(extrator){
        case MAX_TEXT_EXTRACTOR:
            extractor = new MaxTextContentExtractor();
            break;
        case TEXT_DENSITY_EXTRACTOR:
            extractor = new TextDensityExtractor();
            break;
        default:
            throw new IllegalArgumentException("Illegal Extractor defined: value = "+extractor);
        }
        return new HtmlSucker(extractor);
    }

    private ContentExtractor extractor;

    private HtmlSucker(ContentExtractor extractor) {
        this.extractor = extractor;
    }

    /**
     * 根据 URL 来解析文章信息
     * @param document
     * @param saveDiv
     *      验证是否有文章内容
     * @return
     */
    public Article parse(Document document, String pageUrl,String titleDiv,String saveDiv,String pushTime) throws IOException {
//        return parse(Jsoup.parse(new URL(url), timeMillis));
        //hzb改全局使用统一请求方式采集数据
//        Document document = SpiderUtils.getJunshiByNotProxy(url,new Object());
        if(StringUtils.isNotEmpty(saveDiv)){
//            String articlestr = document.select(saveDiv).toString();
           Document saveDoc =  getSaveDiv(document,saveDiv);
            String articlestr = String.valueOf(saveDoc);
            if(StringUtils.isEmpty(articlestr)){
                return null;
            }
        }

        return parse(document,pageUrl,titleDiv,pushTime);
    }

    /**
     * 删除标签
     * @param doc
     * @param saveDiv
     * @return
     */
    public static Document getSaveDiv(Document doc,String saveDiv){
        if(StringUtils.isEmpty(saveDiv)){
            return doc;
        }
        Document document = new Document("");
        String divlsit[] = saveDiv.split(";");
        for(String div:divlsit){
            try {
//                doc.select(div).remove();
                document.append(String.valueOf(doc.select(div)));
            }catch (Exception e){
                System.out.println("获取div失败："+e.getMessage());
            }
        }
        return document;
    }

    /**
     * 根据 html 内容来解析文章信息
//     * @param html
     * @return
     */
//    public Article parse(String html) {
//        return parse(Jsoup.parse(html));
//    }

    private Article parse(Document doc, String pageUrl,String titleDiv,String pushTime) {
        Article art = new Article();
        art.setTitle(MetadataExtractor.title(doc,titleDiv));
        art.setPushTime(MetadataExtractor.pushTime(doc,pushTime));
//        art.setTitle(doc.select(titleDiv));
        art.setDescription(MetadataExtractor.description(doc));
        art.setKeywords(MetadataExtractor.keywords(doc));
        art.setAuthor(MetadataExtractor.author(doc));
        art.setDate(MetadataExtractor.date(doc));
        art.setImage(MetadataExtractor.image(doc));

        //开始解析内容
//        String documentStr = extractor.content(doc.body());
//        String documentStr = "";
//        if(LhStrUtils.isEmpty(documentStr)){
//            documentStr = doc.getElementsByTag("body").html();
//        }
//        art.setContent(documentStr);
        art.setContent(extractor.content(doc.body()));

        if(art.getImage()==null || art.getImage().isEmpty()) {
            Document body = Jsoup.parse(art.getContent(),pageUrl);
            Element img = body.select("img").first();
            if(img != null) {
                String src = img.attr("abs:src");
                if(src == null || src.isEmpty()) {
                    src = img.attr("data-src");
                }
                art.setImage(src);
            }
        }

        return art;
    }

}
